package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC260 {
    public long startExecuteTime = System.currentTimeMillis();

    /*
     * @param 此题目直接用别人代码，自己只理解思想
     * 经典解法，每日一看
     * 奇技淫巧位运算
     * @return:
     */
    class Solution {
        public int[] singleNumber(int[] nums) {
            // difference between two numbers (x and y) which were seen only once
            int bitmask = 0;
            for (int num : nums) bitmask ^= num;

            // rightmost 1-bit diff between x and y
            int diff = bitmask & (-bitmask);

            int x = 0;
            // bitmask which will contain only x
            for (int num : nums) if ((num & diff) != 0) x ^= num;

            return new int[]{x, bitmask ^ x};
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,2,1,3,2,5]");
        System.out.println(solution.singleNumber(arr));
    }

    public static void main(String[] args) throws Exception {
        LC260 an = new LC260();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}

